using System.Collections.Generic;
using UnityEngine;

public class TrainingInfo : MonoBehaviour
{
	public UILabel title;

	public UILabel levelRequired;

	public UILabel price;

	public UILabel energy;

	public UILabel duration;

	public GameObject coinSprite;

	public GameObject ggdollarSprite;

	public List<UILabel> statNames = new List<UILabel>();

	public List<UILabel> boosts = new List<UILabel>();

	public UIWidget trainButtonSprite;

	public UILabel trainButtonLabel;

	public UILabel strengthPrereq;

	public UILabel speedPrereq;

	private TrainingConfig.Training selectedTraining;

	private PlayerTraining trainingManager;

	private Color activeColour = new Color(0.19f, 0.596f, 0f, 1f);

	private Color disabledColour = new Color(0.56f, 0.56f, 0.56f, 1f);

	public void Init(TrainingConfig.Training training)
	{
		selectedTraining = training;
		trainingManager = GGPlayerSettings.instance.statsManager.playerTraining;
		UITools.ChangeText(title, training.trainingName);
		UITools.ChangeText(duration, GGFormat.FormatTimeSpan(trainingManager.GetTrainingDurationForPlayer(training.GetTrainingDuration())));
		UpdatePrerequisites(training);
		UpdatePrice(training);
		UpdateStatBoostInfo(training);
		UpdateTrainButton();
	}

	private void UpdatePrerequisites(TrainingConfig.Training training)
	{
		UITools.ChangeText(levelRequired, training.levelRequired.ToString());
		UITools.ChangeText(strengthPrereq, training.GetAttrPrereq(PlayerStatsManager.AttributeType.Strength).ToString());
		UITools.ChangeText(speedPrereq, training.GetAttrPrereq(PlayerStatsManager.AttributeType.Speed).ToString());
	}

	private void UpdatePrice(TrainingConfig.Training training)
	{
		UITools.ChangeText(price, training.price.ToString());
		coinSprite.SetActive(training.currencyType == CurrencyType.coins);
		ggdollarSprite.SetActive(training.currencyType == CurrencyType.ggdollars);
		UITools.ChangeText(energy, training.energyCost.ToString());
	}

	private void UpdateStatBoostInfo(TrainingConfig.Training training)
	{
		for (int i = 0; i < MaxStatDisplaySize(); i++)
		{
			if (i >= training.statUpdateInfo.Count)
			{
				statNames[i].gameObject.SetActive(false);
				boosts[i].gameObject.SetActive(false);
				continue;
			}
			statNames[i].gameObject.SetActive(true);
			UITools.ChangeText(statNames[i], training.statUpdateInfo[i].GetStatName() + ":");
			boosts[i].gameObject.SetActive(true);
			UITools.ChangeText(boosts[i], "+" + training.statUpdateInfo[i].totalBoost);
		}
	}

	private int MaxStatDisplaySize()
	{
		return statNames.Count;
	}

	private void UpdateTrainButton()
	{
		if (CanTrain())
		{
			trainButtonSprite.color = activeColour;
			UITools.ChangeText(trainButtonLabel, "Train");
		}
		else
		{
			trainButtonSprite.color = disabledColour;
			UITools.ChangeText(trainButtonLabel, "Unavailable");
		}
	}

	private bool CanTrain()
	{
		if (!trainingManager.isTrainingFinished())
		{
			return false;
		}
		if (!HasFunds())
		{
			return false;
		}
		if (BehaviourSingleton<EnergyManager>.instance.GetCurrentEnergyValue() < selectedTraining.energyCost)
		{
			return false;
		}
		return true;
	}

	private bool HasFunds()
	{
		if (selectedTraining.currencyType == CurrencyType.coins && selectedTraining.price > GGPlayerSettings.instance.Model.coins)
		{
			return false;
		}
		if (selectedTraining.currencyType == CurrencyType.ggdollars && selectedTraining.price > GGPlayerSettings.instance.Model.giraffeDollars)
		{
			return false;
		}
		return true;
	}

	private void Update()
	{
		UpdateTrainButton();
	}

	public void OnStartTraining()
	{
		if (!trainingManager.isTrainingFinished())
		{
			TableTennisDialog.instance.ShowOk("Altready Training", "You're already training! wait for the current training to finish!", "Ok", null);
		}
		else if (!HasFunds())
		{
			TableTennisDialog.instance.ShowNotEnoughCoins(selectedTraining.price);
		}
		else if (BehaviourSingleton<EnergyManager>.instance.GetCurrentEnergyValue() < selectedTraining.energyCost)
		{
			TableTennisDialog.instance.ShowYesNo("Not enough energy", "You don't have enough energy.", "Buy energy", "Cancel", delegate(bool complete)
			{
				NavigationManager.instance.Pop();
				if (complete)
				{
					NavigationManager.instance.Pop();
					TableTennisMainNavigation tableTennisMainNavigation = NavigationManager.instance as TableTennisMainNavigation;
					tableTennisMainNavigation.PushItemShop(StoreItemsConfig.StoreItem.ItemType.Food);
				}
			});
		}
		else
		{
			trainingManager.StartTraining(selectedTraining);
			UpdateTrainButton();
			NavigationManager.instance.Pop();
		}
	}
}
